<?php
/*
 * adapted from Multi plugin, original script by 12Christiaan.
 * modified by sowijo_kaskuser:
 */
$this->AddHook('other_work', 'HelpInvDel');
function HelpInvDel($bot) {
    if ($bot->firstrun)
      { //not work if it's first bot cycle
        $Name = "InvDel"; $Version = "0.1"; $Date = "2011-09-20";
        $bot->ld->UpdatePluginVersion($bot, $Name, $Version, $Date );
        return;
      }
    $invdel_active = (array)$bot->ld->GetPlSettings('InvDelActivated');
    if($invdel_active['cb_run_mainpage']!==true){return;}
    $bot->SendMsg('InvDel: Checking Inventory to DELETE Items.');
    $InvDelRemoveGift = (array) $bot->ld->GetPlSettings("InvDelRemoveGift");
    $RemoveGiftKeep = array();
    foreach($InvDelRemoveGift as $Item => $Action){
      if(substr($Item,0,5) == 'Remv_') { $ItemName = substr($Item, 5); $RemoveGiftKeep[$ItemName] = $Action; }
    }
    $selrem = ($bot->ld->GetSelect('SELECT count(*) as cnt FROM sqlite_master where tbl_name="Inventory"'));
    $InvFileTot = array();
        if ($selrem[0]["cnt"] == 1) {
            $selrem = ($bot->ld->GetSelect('SELECT count(*) as cnt FROM Inventory'));
            if ($selrem[0]["cnt"] > 0) {
                $selrem = ($bot->ld->GetSelect('SELECT * FROM Inventory'));
                foreach ($selrem as $n)
                {
                  $ItemName = (string)$n['Item'];
                  if($ItemName == "-"){continue;}
                  $ItemHave = intval($n['Number']);
                  if(array_key_exists($ItemName, $RemoveGiftKeep))
                      { $Keep = intval($RemoveGiftKeep[$ItemName]); } else {$Keep = 9999;}
                  if (is_int($Keep)===false) {$Keep = 9999;}
                  // check if we have to remove.
                  if($ItemHave>$Keep) { // let's remove those.
                    $amount = $ItemHave-$Keep;
                    $bot->SendMsg("Delete Gifts: ".$ItemName." Have:" . $ItemHave . " Keep:" . $Keep );
                    $bot->RemoveFromInventory($ItemName,$amount);
                  }
                }
            }
        }
    $bot->ReloadConfig();
    $bot->ld->SaveProfile2db($bot);
    sleep(2);
    // all done, refresh the plugin
    $bot->pm->RefreshMePlugin("InvDel");
} // end function
?>